class GradesController < ApplicationController

  def index
    if authorize
      @total_score = 0
      @assignments = Assignemnt.find_all_assignemnts()
      @studentgrades = {}
      @assignments.each {|assignment| 
          grade_entry = GradeEntry.get_student_grades(Person.find_by_username(session[:loginname]).id, assignment.id)
          if grade_entry.nil?
            @studentgrades[assignment.id] = "-"
          else
            @studentgrades[assignment.id] = grade_entry.grade
            @total_score += grade_entry.grade
          end 
       }
      
      @total_possible = 0
      @assignments.each {|assignment| @total_possible += assignment.points_possible}
      @total_percentage = sprintf("%.4f",@total_score.to_f / @total_possible).to_f * 100
      
      person = Person.find(:first, :conditions => [ "user = ?", session[:loginname]])
      @firstname = person.firstname
      @lastname = person.surname
      if session[:loginrole] == "Admin"
          @dashboard = "Go to Gradebook"
      end
    end
  end

  def authorize 
	unless session[:loggedin] == true
      flash[:notice] = "Please log in" 
	  redirect_to :controller => :login, :action => :index 
	  return false
	end
	return true
  end

end
